<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @package     Page Module
 * @author      Subha
 * @copyright   (c) 2014, 300 Development
 * @since       Version 0.1
 * @company     Matrix Infoline
 */

class Member_model extends CI_Model
{


	function __construct() {
		parent::__construct();
		log_message('debug', 'Model Initialized');

		$this->load->driver('session');
	}

	//-- CHECK MEMBER LOGGED-IN --//
	function check_logedin() {

		if (!get_session('mem_email')) {
			redirect('member/login');
		}
	}

	//-- CHECK MEMBER LOGIN POST --//
	function check_login_submit($post_data) {
		if ($post_data) {

			$email = trim($post_data['email']);
			$password = trim($post_data['password']);

			$sql = "Select * from member where email='$email' and password='$password' and status=51";
			$query = $this->db->query($sql);
			$user = $query->row();

			if ($query->num_rows() == 1) {
				//write_session("mem_id",NULL);

				write_session('mem_id', $user->id);
				write_session('mem_email', $user->email);
				write_session('mem_password', $user->password);
				write_session('mem_name', $user->name);
				write_session('mem_last_login_date', $user->last_login_date);
				write_session('mem_last_login_ip', $user->last_login_ip);

				//print get_session('mem_id');
				//print $user->email;
				//die();

				//-- Update last login of successfull Login
				$sql = "update member set last_login_date = NOW() where id=$user->id";
				$query = $this->db->query($sql);
				return TRUE;
			} else {
				return FALSE;
			}
		}
	}

	function clear_session_data(){

		clear_session('mem_id');
		clear_session('mem_email');
		clear_session('mem_password');
		clear_session('mem_name');
		clear_session('mem_last_login_date');
		clear_session('mem_last_login_ip');
		clear_session('mem_created_on');

	}

	function upload($imageData=NULL){

		if ($imageData['imagearray']['error'] == 0) {

			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", strtolower($imageData['imagearray']['name']));
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=md5(time());
			//die($imagename);

			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];

			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}

			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].'.'.$ext;
			}
			if($imageData['prefix'])
				$filename=$imageData['prefix'].$filename;

			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];


			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {

				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull



			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{

					$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);

					$img=explode( '.',$filename);
					$imgfile=$img[0];

					$imgfilename = $imgfile.'_thumb'.'.'.$ext;

					//print $filename;
					//die();


					//$thumb['imgFile'] =
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];

					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$imgfilename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$imgfilename,'code'=>1);
				return $imagedata;
			}
		}
	}

	function image_upload($imageData=NULL){

		if ($imageData['imagearray']['error'] == 0) {

			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", strtolower($imageData['imagearray']['name']));
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=md5(time());
			//die($imagename);

			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];

			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}

			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].'.'.$ext;
			}
			if($imageData['prefix'])
				$filename=$imageData['prefix'].$filename;

			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];


			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {

				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull



			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{

					$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);

					$img=explode( '.',$filename);
					$imgfile=$img[0];

					$imgfilename = $imgfile.'_thumb'.'.'.$ext;

					//print $filename;
					//die();


					//$thumb['imgFile'] =
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];

					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$filename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$filename,'code'=>1);
				return $imagedata;
			}
		}
	}

	function checkIdentity($email){
		$sql = "select * from member where email='$email' and identity='FB'";
		$result = $this->db->query($sql)->result();
		if($result){
			return TRUE;
		}else{
			return FALSE;
		}
	}


	public function post_logo($post_id) {
		$sql = "select * from post where id='$post_id' ";
		$query = $this->db->query($sql);
		$row = $query->row();
		if($row){
			return $row;
		}else{
			return false;
		}
	}

	public function getGallerydata($post_id=NULL,$term=NULL) {
		$sql = "select * from gallery where post_id='$post_id' and term='$term'";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}


	public function getBusiness($mem_id=NULL) {
		$sql = "select * from post where mem_id='$mem_id'";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	function getPostdata($post_id=0,$value=0,$term=NULL){
		$sql = "select * from term where post_id='$post_id' and value='$value' and term='$term'";
		$query = $this->db->query($sql);
		if($query->num_rows()!=0){
			return $query->row()->value;
		}else{
			return 0;
		}

			
			
	}


	function getTemplatebyType($variable) {
		$sql = "select * from email_template where variable='$variable' ";
		$result = $this->db->query($sql)->row();
		if($result){
			return $result;
		}else{
			return false;
		}
	}
	function getMemberbyemail($email){
		if($email){
			$sql="select * from member where email='$email'";
			$query = $this->db->query($sql);
			if ($query->row())
				return $query->row();
			else
				return false;
		}else{
			return false;
		}
	}

	function getWebconfiguration($fieldName) {
		$sql = "SELECT $fieldName FROM web_settings";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		return $row->$fieldName;
	}

	function send_mail($email = NULL, $subject = NULL, $body = NULL) {

		$this->email->clear();
		//--Email Config
		$email_config['protocol'] = 'mail';
		$email_config['wordwrap'] = FALSE;
		$email_config['mailtype'] = 'html';
		$email_config['charset'] = 'utf-8';
		$email_config['crlf'] = "\r\n";
		$email_config['newline'] = "\r\n";
		$this->email->initialize($email_config);

		$this->email->to($email);
		$this->email->from($this->member_model->getWebconfiguration('default_email'), $this->member_model->getWebconfiguration('web_title'));
		$this->email->subject($subject);
		$this->email->message($body);
		$this->email->send();
	}

	function membershipFacility($post_id, $fieldName) {
	
		$this->db->select('plan');
		$this->db->where('id', $post_id);
		$query = $this->db->get('post');
		$mem_id = $query->row()->plan;
	
		$sql = "SELECT $fieldName FROM membership where id=$mem_id";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		if($row){
		return $row->$fieldName;
		}else{
			return FALSE;
		}
	}
	function delete_photo($id,$image_file) {
		if($id && $image_file){
			$sql="delete from gallery where id=$id";
	
			$this->db->query($sql);
			//--Delete Image File --//
	
			$f = BASEPATH.'../assets/img/data/photo/'.$image_file;
			unlink($f);
		}
	}
}